﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Complaint.Models.Dtos;
using Complaint.Models.Services;
using Complaint.Repositories;

namespace Complaint.Models
{
    public class CommentModel : BaseModel<CommentRepository>, IModel<Comment>
    {
        #region Implementation of IModel<Comment>

        /// <summary>
        /// Retrieves all instances of the type T.
        /// </summary>
        /// <returns>The requested instances.</returns>
        public IList<Comment> Retrieve()
        {
            return Repository.FindAll().ToList();
        }

        /// <summary>
        /// Creates the passed dto in the data store.
        /// </summary>
        /// <param name="dto">The data transfer object containing the new values.</param>
        public void Create(Comment dto)
        {
            if (dto != null) Repository.Create(dto);
        }

        /// <summary>
        /// Updates the passed dto in the data store.
        /// </summary>
        /// <param name="dto">The data transfer object containing the new values.</param>
        public void Update(Comment dto)
        {
            if (dto != null) Repository.Update(dto);
        }

        /// <summary>
        /// Deletes the passed dto from the data store.
        /// </summary>
        /// <param name="dto">The data transfer object to delete.></param>
        public void Delete(Comment dto)
        {
            if (dto != null) Repository.Delete(dto);
        }

        /// <summary>
        /// Retrieves all instances of the type T with the matching name.
        /// </summary>
        /// <param name="name">The name of the instances to retrieve.</param>
        /// <returns>The requested instances.</returns>
        public IList<Comment> FindByName(string name)
        {
            return Repository.FindByName(name).ToList();
        }

        /// <summary>
        /// Retrieves an instances of the type T with the matching id.
        /// </summary>
        /// <param name="id">The Id of the instance to retrieve.</param>
        /// <returns>The requested instances.</returns>
        public Comment FindById(int id)
        {
            return Repository.FindById(id);
        }

        #endregion

        public IList<Comment> Retrieve(bool status)
        {
            return Repository.FindAll(status).ToList();
        }

        public IList<Comment> FindByComplaintId(int id)
        {
            return Repository.FindByComplaintId(id).ToList();
        }

        public void Report(int id, string from, string to)
        {
            var comment = Repository.FindById(id);
            //ToDo:  compose report email and send it
            string message = ComposeReportEmail(comment);

            var subject = string.Format("Web user report comment #{0}", id);

            var sendMailThread = new Thread(() => new MailService().Send(
                                                          from,
                                                          to,
                                                          subject,
                                                          message
                                                          )) {IsBackground = true};

            sendMailThread.Start();
        }

        private static string ComposeReportEmail(Comment comment)
        {
            var sb = new StringBuilder();
            sb.AppendLine("Following comment has been reported as inappropriate, please investigate.\n");
            sb.AppendFormat("Author: {0}", comment.Name);
            sb.AppendLine();
            sb.AppendFormat("Email: {0}", comment.Email);
            sb.AppendLine();
            sb.AppendFormat("Date added: {0}", comment.DateAdded);
            sb.AppendLine();
            sb.AppendFormat("Comment: {0}", comment.Comment1);
            return sb.ToString();
        }
    }
}
